const router = require('koa-router')(),
      mysql = require('mysql2/promise'),
      { plan } = require('../models'),
      log = console.log;

router.prefix('/api/kdoing/plan');

router.get('/getplans/:cla_id', async (ctx, next) => {
  const {cla_id} = ctx.params;
  ctx.body = await plan.getPlans(cla_id);
});
router.post('/makeplan', async (ctx, next) => {
  var {user_id, height, weight, reducedfat, musclebuild, weatherrun, daysaweek, minsaday} = ctx.request.body;
  ctx.body = await plan.makePlan(user_id, height, weight, reducedfat, musclebuild, weatherrun, daysaweek, minsaday);
});
router.get('/getchildplan/:plan_id', async (ctx, next) => {
  var { plan_id } = ctx.params; 
  ctx.body = await plan.getChildplanByPlanid(plan_id);
});
router.get('/getaplan/:plan_id', async (ctx, next) => {
  var { plan_id } = ctx.params; 
  ctx.body = await plan.getAPlan(plan_id);
});
router.get('/getallplans', async (ctx, next) => {
  ctx.body = await plan.getAllPlans();
});
router.post('/adduserplan', async (ctx, next) => {
  var {user_id, plan_id} = ctx.request.body;
  ctx.body = await plan.addUserPlan(user_id, plan_id);
});
router.delete('/deluserplan', async (ctx, next) => {
  var {user_id, plan_id} = ctx.request.body;
  ctx.body = await plan.delUserPlan(user_id, plan_id);
});
router.get('/getuserplans/:user_id', async (ctx, next) => {
  var { user_id } = ctx.params; 
  ctx.body = await plan.getUserPlans(user_id);
});
router.delete('/delplan/:plan_id', async (ctx, next) => {
  var {plan_id} = ctx.params;
  ctx.body = await plan.delPlan(plan_id);
});
router.post('/addchildplan', async (ctx, next) => {
  var {plan_id, cp_id, cp_name, cp_time, cp_week, cp_day} = ctx.request.body;
  ctx.body = await plan.addChildPlan(plan_id, cp_id, cp_name, cp_time, cp_week, cp_day);
});

router.delete('/delchildplan', async (ctx, next) => {
  var {plan_id, cp_id} = ctx.request.body;
  ctx.body = await plan.delChildPlan(plan_id, cp_id);
});



module.exports = router;
